﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using DungChung;
namespace CoVuaClient
{
    class QuanLiKetNoiClient
    {
        private TaiKhoanDTO _taiKhoan;

        public TaiKhoanDTO TaiKhoan
        {
            get { return _taiKhoan; }
            set { _taiKhoan = value; }
        }

        public TcpClient Client
        {
            get { return _Client; }
            set { _Client = value; }
        }

        public ShowItem ShowItem
        {
            get { return showItem; }
            set { showItem = value; }
        }

        public ChatShowList ChatShowList
        {
            get { return _chatShowList; }
            set { _chatShowList = value; }
        }

        public NetworkStream Stream
        {
            get { return _stream; }
            set { _stream = value; }
        }

        public BinaryReader Reader
        {
            get { return reader; }
            set { reader = value; }
        }

        public BinaryWriter Writer
        {
            get { return writer; }
            set { writer = value; }
        }

        private ChatShowList _chatShowList;
        private NetworkStream _stream;
        private BinaryReader reader;
        private BinaryWriter writer;
        private TcpClient _Client;
        private ShowItem showItem;
        public QuanLiKetNoiClient(TcpClient client)
        {
            Client = client;
            Client.ReceiveTimeout = 50;
            Stream = client.GetStream();
            Writer = new BinaryWriter(Stream);
            Reader = new BinaryReader(Stream);
            TaiKhoan = new TaiKhoanDTO();
            TaiKhoan.TenHienThi = Reader.ReadString();
            TaiKhoan.Status = Reader.ReadString();
            TaiKhoan.KinhNghiem = Reader.ReadInt32();
            TaiKhoan.GioiThieu = Reader.ReadString();
            showItem = new ShowItem();
            ChatShowList = new ChatShowList();
        }
        public QuanLiKetNoiClient(TcpClient client, NetworkStream stream)
        {
            Client = client;
            _stream = stream;
            Writer = new BinaryWriter(_stream);
            Reader = new BinaryReader(_stream);
        }
        public string RecievePacketInfo()
        {
            return Reader.ReadString();
        }
        public Item RecieveUserInfo()
        {
            Item item = new Item();
            item.Username = Reader.ReadString();
            item.TenHienThi = Reader.ReadString();
            item.Status = Reader.ReadString();
            item.KinhNghiem = Reader.ReadInt32().ToString("000000");
            item.GioiThieu = Reader.ReadString();
            if (Reader.ReadBoolean())
            {
                item.UserState = "Đang rảnh tay!";
            }
            else
            {
                item.UserState = "Đã có người chơi cùng rồi!";
            }
            return item;
        }
        public void ReceiveListUser()
        {
            int soLuong = Reader.ReadInt32();
            for (int i = 0; i < soLuong; i++)
            {
                Item item = new Item();
                item = RecieveUserInfo();
                item.ThuTu = (i + 1).ToString("00");
                ShowItem.Add(item);
            }
        }
        public void DongKetNoi()
        {
            Writer.Write(MyTask.CLOSE);
            Client.Close();
        }
        public void GuiLoiMoi(string username)
        {
            Writer.Write(MyTask.LoiMoiThamGiaVanDau);
            Writer.Write(username);
        }

        public void TuChoiLoiMoi(string username)
        {
            Writer.Write(MyTask.TuChoi);
            Writer.Write(username);
        }
        public void NhanLoiMoi(string username)
        {
            Writer.Write(MyTask.NhanLoiMoi);
            Writer.Write(username);
        }
        public void GuiTinNhanChat(string NoiDung)
        {
            Writer.Write(MyTask.Chat);
            Writer.Write(NoiDung);
        }
        public void DanhSachNguoiChoiCaoDiem()
        {
            Writer.Write(MyTask.XemDanhSach);
        }
        public void ChapNhanThuaCuoc()
        {
            Writer.Write(MyTask.XinThua);
        }
        public void SendNuocDi(int ChiSoCot1, int chiSoDong1, int chiSotCot2, int chiSoDong2, bool chieuTuong)
        {
            Writer.Write(MyTask.NuocDi);
            Writer.Write(ChiSoCot1);
            Writer.Write(chiSoDong1);
            Writer.Write(chiSotCot2);
            Writer.Write(chiSoDong2);
            writer.Write(chieuTuong);
        }
        public int ChienThang()
        {
            writer.Write(MyTask.ChienThang);
            int kinhNghiem = reader.ReadInt32();
            return kinhNghiem;
        }
    }
}
